//
//  Fraction.m
//  OC03_属性_点语法
//
//  Created by dllo on 16/2/26.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "Fraction.h"

@implementation Fraction
#pragma mark - init
- (instancetype)initWithNumetator:(NSInteger)numetator denominator:(NSInteger)denominator{
    self = [super init];
    if (self) {
        _numetator = numetator;
        _denominantor = denominator;
    }
    return self;
}
#pragma mark - 构造器
+ (instancetype)fractionWithNumetator:(NSInteger)numetator denominator:(NSInteger)denominator{
    return [[Fraction alloc] initWithNumetator:numetator denominator:denominator];
   
}
#pragma mark - 功能
- (Fraction *)plusWithFraction:(Fraction *)frac{
    //新的分数分子
    NSInteger newNum = self.numetator * frac.denominantor + self.denominantor * frac.numetator;
    //新的分数分母
    NSInteger newDeno = self.denominantor * frac.denominantor;
    // 创建一个分数对象.
    Fraction *newFrac = [Fraction fractionWithNumetator:newNum denominator:newDeno];
    //返回分数
    return newFrac;
    
    
    
}
- (Fraction *) minusWithFractio:(Fraction *)frac{
    NSInteger newNum = self.numetator * frac.denominantor - self.denominantor * frac.numetator;
    NSInteger newDeno = self.denominantor * frac.denominantor;
    Fraction *newFrac = [Fraction fractionWithNumetator:newNum denominator:newDeno];
    return newFrac;
}
- (Fraction *)mulWithFraction:(Fraction *)frac{
    NSInteger newNum = self.numetator * frac.numetator;
    NSInteger newDon = self.denominantor * frac. denominantor;
    Fraction *newFrac = [Fraction fractionWithNumetator:newNum denominator:newDon];
    return newFrac;
}

- (Fraction *)divWithFraction:(Fraction *)frac{
    NSInteger newNum = self.numetator * frac.denominantor;
    NSInteger newDon = self.denominantor * frac.numetator;
    Fraction *newFrac = [Fraction fractionWithNumetator:newNum denominator:newDon];
    return newFrac;
}

- (Fraction *)compareWithFracton:(Fraction *)frac{
    NSInteger num1 = self.numetator * frac.denominantor;
    NSInteger num2 = self.denominantor * frac.numetator;
    NSInteger newDon = self.denominantor * frac.denominantor;
    if (num1 > num2) {
        Fraction *newFrac = [Fraction fractionWithNumetator:num1 denominator:newDon];
        return newFrac;
    }
    else {
        Fraction *newFrac = [Fraction fractionWithNumetator:num1 denominator:newDon];
        return newFrac;
    }
    
    
}
- (Fraction *)yueWithFraction:(Fraction *)frac{
    NSInteger  min1 = self.denominantor < self.numetator ? self.denominantor : self.numetator;
    NSInteger min2 = frac.denominantor < frac.numetator ? frac.denominantor : frac.numetator;
    for (NSInteger i = min1; i > 0; i--) {
        if (self.denominantor % i == 0 && self.numetator % i ==0) {
            NSInteger newNum = self.numetator / i;
            NSInteger newDon = self.denominantor / i;
            Fraction *newFrac = [Fraction fractionWithNumetator:newNum denominator:newDon];
            return newFrac;
        }
    }
    for (NSInteger j = min2; j > 0; j--) {
            if (frac.denominantor % j == 0 && frac.numetator % j ==0) {
                NSInteger newNum = frac.numetator / j;
                NSInteger newDon = frac.denominantor / j;
                Fraction *newFrac = [Fraction fractionWithNumetator:newNum denominator:newDon];
                return newFrac;
            }

        }
    return 0;
    
}



@end

